package com.crrc;

import com.crrc.util.FileParseParam;
import com.crrc.util.OfficeToJudge;
import com.crrc.util.PictureWatermark;
import com.crrc.util.ReadOfficeUtil;

import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import javax.imageio.stream.MemoryCacheImageInputStream;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 *  在接收值为byte数据类型的情况下，在内存中处理文件
 *  解析文件文字内容
 *  解析图片内容
 *  解析图片文字内容
 */
public class CrrCApplicaiton {
    public static void  main(String[] args) throws IOException {
        String str2 = "D:\\english_path\\1.png";
        String str3 = "D:\\english_path\\2.jpg";
        byte[] buffer = null;
        String[] str5  = new String[]{str2,str3};
        for(String s : str5){
            File file = new File(s);
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] b = new byte[1024];
            int n;
            while ((n = fis.read(b)) != -1)
            {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
            System.out.println(buffer);
            FileParseParam fileParseParam = new FileParseParam();
            File file1 = PictureWatermark.pictureWatermark(buffer,"txt");
        }

        //string 转 byte[]
       /* String str1 = "D:\\english_path\\new.docx";
        String str2 = "D:\\english_path\\new.pptx";
        String str3 = "D:\\english_path\\new.doc";
        String str4 = "D:\\english_path\\new.ppt";
        String str = "D:\\english_path\\SequoiaDB_v3.4_技术文档.pdf";

        String[] str5  = new String[]{str4,str1,str2,str3,str};
        byte[] buffer = null;
        for(String s : str5){
            File file = new File(s);
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] b = new byte[1024];
            int n;
            while ((n = fis.read(b)) != -1)
            {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();

            FileParseParam fileParseParam = new FileParseParam();
            parseFile(buffer,fileParseParam,file);
        }*/



    }

    private static BufferedImage readMemoryImage(byte[] buffer) throws IOException {
        if (null == buffer || 0 == buffer.length) {
            throw new NullPointerException("the argument 'imgBytes' must not be null or empty");
        }
        // 将字节数组转为InputStream，再转为MemoryCacheImageInputStream
        ImageInputStream imageInputstream = new MemoryCacheImageInputStream(new ByteArrayInputStream(buffer));
        // 获取所有能识别数据流格式的ImageReader对象
        Iterator<ImageReader> it = ImageIO.getImageReaders(imageInputstream);
        // 迭代器遍历尝试用ImageReader对象进行解码
        while (it.hasNext()) {
            ImageReader imageReader = it.next();
            // 设置解码器的输入流
            imageReader.setInput(imageInputstream, true, true);
            // 图像文件格式后缀
            String suffix = imageReader.getFormatName().trim().toLowerCase();
            // 图像宽度
            int width = imageReader.getWidth(0);
            // 图像高度
            int height = imageReader.getHeight(0);
            System.out.printf("format %s,%dx%d\n", suffix, width, height);
            try {
                // 解码成功返回BufferedImage对象
                // 0即为对第0张图像解码(gif格式会有多张图像),前面获取宽度高度的方法中的参数0也是同样的意思
                return imageReader.read(0, imageReader.getDefaultReadParam());
            } catch (Exception e) {
                imageReader.dispose();
                // 如果解码失败尝试用下一个ImageReader解码
            }
        }
        imageInputstream.close();
        // 没有能识别此数据的图像ImageReader对象，抛出异常
        throw new IOException("unsupported image format");
    }


    public static FileParseParam parseFile(byte[] srtbyte, FileParseParam fileParseParam, File file) throws IOException{
        String content = null;
        String text = "大白先生";
        List<File> fileName = new ArrayList<File>();
        //读取文字内容
        try{
            content  = ReadOfficeUtil.readOffice(srtbyte);
            System.out.println(file+"文件文字："+content);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
        //放入文字内容
        fileParseParam.setParsingWords(content);
        //根据文件后缀名跳入提取文件图片方法
        List<File> list = null;
        try{
            list = OfficeToJudge.formatConversionOffice(srtbyte,file);
            System.out.println(list);
        }catch (Exception e){
            throw new FileNotFoundException(e.getMessage());
        }for(File pictureName : list){
            //遍历后将获取到的文件路径依次放到fileParseParam类型的参数中
            fileName.add(pictureName);
        }
        fileParseParam.setStorePath(fileName);
        System.out.println(fileParseParam.toString());
        return fileParseParam;
    }
}
